<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
$msg = '';

function filter_post(&$data) {
    $remove = array('#<[^>][0,]>#');

    foreach ($data as $key => $item) {
        $data[$key] = preg_replace($remove, '', $item);
    }
}

$katStr = explode(';', preg_replace('#;$#','',trim(ZAPYTANIE_KATEGORIE)));

foreach($katStr as $idx => $kat){
    
    $katStr[preg_replace(array('#[^a-z0-9]+#', '#\s#', ), array('', '_', ),  strtolower($kat))] = $kat;
    unset($katStr[$idx]);
}

function validate($data, &$msg) {

    $map = array(
        'firma' => array('#[a-z\s]{3,}#i', 'Podaj Firmę'),
        'email' => array('#[a-z0-9@\.\-_]{12,}#i', 'Podaj prawidłowy e-mail'),
        'telefon' => array('#[a-z0-9\s\.\-_]{7,}#i', 'Podaj telefon'),
        'kategoria' => array('#(?!=wybierz)#i', 'Wybierz kategorie'),
        'tresc' => array('#[a-z0-9\s\.\-_]{50,}#i', 'Wpisz treść.inimum 50 naków. Podałeś ' . strlen($data['tresc'])
        )
    );

    $return = true;
    foreach ($data as $key => $item) {

        if (isset($map[$key])) {

            $pas = preg_match($map[$key][0], $item);

            if ($pas == 0) {
                $return = false;
                $msg = $msg . '<span>' . $map[$key][1] . '</span><br />';
            }
        }
    }
    if($msg=="")
        $msg = 'Mail został wyslany. Dziekujemy.';
    return $return;
}

$is_post = count($_POST) > 0 && isset($_POST['sendMail']);
$valid = 0;
if ($is_post) {

    filter_post($_POST);

    $valid = validate($_POST, $msg);
    if ($valid) {


        $headers[] = 'From: Konserwator';

        $to = MAIL_TO;

        mail($to, 'Kontakt zapytanie ofertowe:' . $_POST['temat'], 'Użytkownik ' . $_POST['imie'] . ', emial: ' . $_POST['email'] . PHP_EOL .
                'Wpisał taką treść' . PHP_EOL . $_POST['tresc'] . str_repeat(PHP_EOL, 3) .
                'Data: ' . date('Y m d H:i:s ', time()) . ' ADRES IP:' . $_SERVER['REMOTE_ADDR'], implode("\r\n",$headers));
    }
}
?>
<?php if ($msg != "") { ?>
    <div class="info errors"><?php echo $msg; ?></div>
    <script type="text/javascript">setTimeout(function(){window.location.assign(document.URL)},2000)</script>
<? } ?>
<div class="zapytanie_div">
    <div class="zapytanie_wrap">
        <div>

            <div class="fl" id="zapytanie_witget">

                <h1 class="page-title">Zapytanie ofertowe</h1>

                <div>
                    <?php echo $this->tresc['short'] ?>
                </div>
            </div>
            <div class="cls"></div>

            <div id="zapytanie_form" class="form">

                <form method="POST" action="" id="formzapytanie">
                    <div class="k_t">
                        <div class="fl">
                            <span>Firma<span class="required">*</span></span>
                        </div>
                        <div class="fl">
                            <span>E-mail<span class="required">*</span></span>
                        </div>
                        <div class="fl">
                            <span>Telefon<span class="required">*</span></span>
                        </div>
                        <div class="cls"></div>
                    </div>
                    
                    <div class="k_t">
                        <div class="fl">
                            <input type="text" name="firma">
                        </div>
                        <div class="fl">
                            <input type="text" name="email">
                        </div>
                        <div class="fl">
                            <input type="text" name="telefon">
                        </div>
                        <div class="cls"></div>
                    </div>
                    
                    <div class="k_t">
                        <div class="fl" style="width: 425px; margin-right: 25px">
                            <span>Stanowisko</span>
                        </div>
                        <div class="fl">
                            <span>Kategoria<span class="required">*</span></span>
                        </div>
                        <div class="cls"></div>
                    </div>
                    <div class="k_t">
                        <div class="fl" style="width: 450px">
                            <input type="text" name="stanowisko" style="width: 425px;">
                        </div>
                        <div class="fl">
                            <select name="kategoria" style="width: 200px">
                                <option>wybierz kategorie</option>
                                <?php foreach($katStr as $katname => $kat){?>
                                <option value="<?php echo $katname?>"><?php echo $kat?></option>
                                <?php }?>
                            </select>
                        </div>
                        <div class="cls"></div>
                    </div>
                    <div style="margin-left: 10px; padding-top: 15px">
                        <p>Wiadomość<span class="required">*</span></p>
                        <textarea name="tresc" cols="57" rows="12"></textarea><br>
                        <div style="text-align: right;width:486px; padding: 6px 0 20px">
                            <input id="send_form" type="submit" value="Wyślij" class="buttonSend">
                        </div>
                    </div>
                    <input type="hidden" value=" " name="sendMail">
                </form>
            </div>

            <div class="cls"></div>
        </div>
        <div >

        </div>
    </div>
</div>
<script type="text/javascript">
    $('#send_form')
    .click(function(ev){
        ev.preventDefault();
        var valid = true;
        var msg = '';
        
        if($('input[name="firma"]').val().toString().length<2){
            msg += "Wpisz nazwę firmy \r\n";
            valid = false;            
        }
        if($('input[name="email"]').val().toString().length<2){
            msg += "Wpisz E-mail \r\n";
            valid = false;            
        }
        if($('input[name="telefon"]').val().toString().length<2){
            msg += "Wpisz telefon \r\n";
            valid = false;            
        }
        if(/wybierz/.test($('select[name="kategoria"]').val())){
            msg += "Wybierz kategorie \r\n";
            valid = false;            
        }

        if($('textarea[name="tresc"]').val().toString().length<2){
            msg += "Wpisz treść \r\n";
            valid = false;            
        }
        
        if(!valid){
            alert(msg);
            return false;
        }
        $('#formzapytanie').submit()
    });
</script>